<template>
  <div class="commu-detail">
    <!-- TODO:左侧文章 -->
    <div class="article-cont">
      <!-- 标题 -->
      <h1 class="title">{{article.articleTitle}}</h1>
      <!-- 文章发布基本信息 -->
      <div class="aticle-view">By
        <span class="author-name">{{article.nickname}}</span>
        <span class="times">{{article.createTime}}</span>
      </div>
      <!-- 文章具体内容 -->
      <div class="contents"
           v-html="article.articleContent">
      </div>

      <!-- 文章收藏 -->
      <div class="collection">
        <div class="collection-btn"
             @click="getColl(article.articleId)">
          <span>收藏该文章:</span>
          <i class="el-icon-star-on"
             v-if="article.isCollect == 0"></i>
          <i class="el-icon-star-on solid"
             v-else></i>
        </div>
      </div>
      <!-- 评论 -->
      <div class="comment">
        <!-- 评论输入框 -->
        <div class="comment-input">
          <textarea id="sub-txt"
                    v-model.trim="reply.content"></textarea>
          <div class="sub-btn"
               @click="pulishComment(article.articleId)">发表评论</div>
        </div>
        <!-- 评论列表 -->
        <div class="comment-list">
          <h3>评论列表</h3>
          <comment-item v-for="item in comments"
                        :key="item.id"
                        :commentobj="item"
                        @getNewComments='initComments'
                        :articleId="article.articleId" />
        </div>
      </div>
    </div>
    <!-- TODO:右侧作者信息 -->
    <div class="author-message">
      <div class="person">
        <img :src="article.avatar"
             alt="">
        <h2>{{article.nickname}}</h2>
        <div class="person-sign">
          {{article.sign}}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import commentItem from '@/components/CommentItem.vue'
import { detailArticle, addCollect, delCollect, pubComment, getComments } from '@/api/article'
import { getToken } from '@/request/token'
export default {
  components: {
    commentItem
  },
  data() {
    return {
      article: {}, // 文章内容
      comments: [], // 评论
      reply: {
        // 评论回复对象
        aid: '', // 用户id
        content: '', //内容
        pid: '', // 评论的id
        toUserId: '' // 回复用户id
      }
    }
  },
  methods: {
    //文章收藏
    async getColl(id) {
      if (getToken()) {
        if (this.article.isCollect == 0) {
          // 收藏
          let addres = await addCollect(id)
          this.initDeatil()
        } else {
          // 取消
          let delres = await delCollect(id)
          this.initDeatil()
        }
      } else {
        this.$message('warning', '请先登录', 1000)
      }
    },
    // 发表评论
    async pulishComment(id) {
      if (getToken()) {
        if (this.reply.content == '') {
          this.$message('warning', '请输入内容', 1000)
          return false
        }
        this.reply.aid = id
        let { data: res } = await pubComment(this.reply) // 发表
        this.initComments() // 获取评论
        this.reply.content = '' // 清空输入框内容
      } else {
        this.$message('warning', '请先登录', 1000)
      }
    },
    // 初始化文章内容
    async initDeatil() {
      let { data: res } = await detailArticle(this.$route.query.articleId)
      console.log(res.data)
      this.article = res.data
    },
    // 初始化评论列表
    async initComments() {
      let { data: res } = await getComments(this.$route.query.articleId)
      console.log(res.data)
      this.comments = res.data
    }
  },
  created() {
    this.initDeatil()
    this.initComments()
  }
}
</script>

<style lang="less" scoped>
.commu-detail {
  display: flex;
  justify-content: space-between;
  margin: 30px auto;
  width: 950px;
  /* 左侧文章 */
  .article-cont {
    width: 655px;
    min-height: 300px;
    /* 文章标题 */
    .title {
      font-size: 24px;
      font-weight: normal;
      font-family: '黑体';
    }
    /* 文章发布基本信息 */
    .aticle-view {
      padding: 15px 0;
      height: 20px;
      line-height: 20px;
      font-size: 14px;
      color: #999;
      border-bottom: 1px solid #cccc;
      .author-name {
        padding: 0 15px 0 10px;
        color: #33c5c5;
      }
      .times {
        color: #999;
      }
    }
    /* 文章实际内容 */
    .contents {
      margin: 20px 0 0 0;
      text-indent: 2em;
      line-height: 30px;
      letter-spacing: 1px;
      .el-avatar {
        display: block;
        margin: 20px auto;
        width: 90%;
      }
    }
    /* 文章收藏 */
    .collection-btn {
      margin-top: 20px;
      display: flex;
      align-items: center;
      height: 55px;
      border-top: 1px solid #ccc;
      i {
        font-size: 28px;
        cursor: pointer;
        color: #999999;
        &:hover {
          color: #33c5c5;
        }
      }
      .solid {
        color: #33c5c5;
      }
    }
    /* 文章评论 */
    .comment {
      margin: 30px 0 70px 0;
      /* 评论输入框 */
      .comment-input {
        margin: 0 20px;
        display: flex;
        justify-content: space-between;
        textarea {
          width: 480px;
          height: 50px;
          padding: 10px;
          background: #f8f8f8;
          border: 1px solid #dcdcdc;
          outline: none;
          resize: none;
          border-radius: 3px;
          box-sizing: border-box;
          &:focus {
            border: 1px solid #33c5c5;
            border-radius: 3px;
          }
        }
        /* 发表按钮 */
        .sub-btn {
          width: 85px;
          line-height: 50px;
          text-align: center;
          font-size: 15px;
          color: #fff;
          cursor: pointer;
          box-sizing: border-box;
          background-color: #33c5c5;
          border-radius: 3px;
        }
      }
      /* 文章列表 */
      .comment-list {
        margin-top: 40px;
        min-height: 300px;
        h3 {
          border-bottom: 1px solid #ccc;
          padding: 10px 0;
        }
      }
    }
  }
  /* 右侧作者信息 */
  .author-message {
    width: 270px;
    min-height: 300px;
    .person {
      img {
        display: block;
        margin: 40px auto 0 auto;
        width: 90px;
        height: 90px;
        border-radius: 50%;
      }
      h2 {
        margin: 15px 0 20px 0;
        font-size: 18px;
        text-align: center;
        color: #33c5c5;
        cursor: pointer;
      }
      .person-sign {
        margin: 0 15px;
        font-size: 13px;
        line-height: 20px;
        font-family: '';
        letter-spacing: 2px;
        text-align: center;
      }
    }
  }
}
</style>